﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using LY.Entity;
using LY.Entity.SearchEntity;
using LY.Entity.PageSearch;
using LY.Entity.Jsons;
using LY.Interface;
namespace QJ.CMS.Areas.Admin.Controllers
{
    public class LinksController : Controller
    {
        //
        // GET: /Admin/Links/

        private static IlinksService _linksservice;
        public LinksController(IlinksService lService)
        {
            _linksservice = lService;
        }

        public ActionResult Index()
        {
            return View();
        }

        //列表信息详细显示
        [HttpPost]
        public ActionResult LinksList(LinksSearch search, FormCollection form)
        {
            PageView view = new PageView(form);
            string colkey = form["colkey"];
            string colsinfo = form["colsinfo"];
            PagedList<C_Links> lList = _linksservice.QueryLinksList(search, view);
            JsonQTable at = JsonQTable.ConvertFromPagedList<C_Links>(lList, colkey, colsinfo.Split(','));
            return Json(at);
        }

        //添加
        /// <summary>
        /// 链接新增或编辑操作，返回不同状态界面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult LinksEdit(int? id)
        {
            C_Links links = null;
            if (id.HasValue)
            {
                links = _linksservice.GetLinks(id);
                links.AddTime = DateTime.Now;
            }
            else
            {
                links = new C_Links();
                links.AddTime = DateTime.Now;
            }
            return View(links);
        }
        //新增或编辑链接信息
        [HttpPost]
        [ValidateInput(false)]
        public JsonResult LinksSave(C_Links links, FormCollection collection)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                //string errmsg = "";
                    int ret = _linksservice.SaveLinks(links);
                    if (ret > 0)
                    {
                        rMsg.Status = 1;
                        rMsg.Message = "操作成功！";
                    }
                    else
                    {
                        rMsg.Status = -1;
                        rMsg.Message = "意外错误，请稍后重试！";
                    }
            }
            catch (Exception ex)
            {
                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }
        //删除链接
        [HttpPost]
        public JsonResult LinksDelete(int id)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                if (id <= 0)
                {
                    rMsg.Status = -1;
                    rMsg.Message = "参数错误！";
                }

                int ret = _linksservice.DeleteLinks(id);
              

                if (ret > 0)
                {
                    rMsg.Status = 1;
                    rMsg.Message = "操作成功！";
                }
                else
                {
                    rMsg.Status = -1;
                    rMsg.Message = "意外错误，请稍后重试！";
                }
            }
            catch (Exception ex)
            {

                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }

    }
}
